//
//  BQShopCartViewController.m
//  BeeQuick
//
//  Created by Vivitickey on 2017/4/26.
//  Copyright © 2017年 test. All rights reserved.
//

#import "BQShopCartViewController.h"
#import "UIColor+HMAddition.h"
#import "BQShopCarFirstCell.h"
#import "BQShopCarSecondCell.h"
#import "BQShopCarThirdCell.h"
#import "BQShopCarFourthCell.h"
#import "BQShopCarFifthCell.h"
#import "BQShopingCarModel.h"
#import "BQAdressModel.h"
#import "BQAdressVC.h"
@interface BQShopCartViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,weak) UIView *headerView;

@property (nonatomic,weak) UIView *footView;

@property (nonatomic,weak) UITableView *shopCarTableView;

@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,weak) UILabel *moneyLabel;

@property (nonatomic,weak) UIView *emptyView;

@end

static NSString *first = @"first";
static NSString *second = @"second";
static NSString *third = @"third";
static NSString *fourth = @"fourth";
static NSString *fifth = @"fifth";


@implementation BQShopCartViewController{
    
    NSArray *_adressArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self registerNotification];
    
    [self loadData];
    
    self.view.backgroundColor = [UIColor hm_colorWithR:239 G:239 B:239];
    
    self.navigationItem.title = @"购物车";
    

}

-(void)viewWillAppear:(BOOL)animated {
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    
}

-(void)setupUI{
    
    UIImage *image =[UIImage imageNamed:@"v2_goback-1"];
    UIButton *btn =[[UIButton alloc]init];
    [btn  setBackgroundImage:image forState:UIControlStateNormal];
    btn.frame =CGRectMake(0, 0, 24, 24);
    
    UIBarButtonItem *button =[[UIBarButtonItem alloc]initWithCustomView:btn];
    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem =button;
    
    [self setFooderView];
    
    [self setupEmptyView];
    
    [self setUpTableView];
    
    
    if (_dataArray.count > 0) {
        self.shopCarTableView.hidden =   NO;
        self.emptyView.hidden = YES;
    }else{
        self.shopCarTableView.hidden =  YES;
        self.emptyView.hidden = NO;
    }
    
}

#pragma mark - 注册通知
- (void)registerNotification {
    
    //购物车通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didClickShopingCarButton) name:kShopingCarNotification object:nil];
}

#pragma mark - 购物车通知

-(void)didClickShopingCarButton{
    
    [self getShopingArray];
    
}

-(void)click{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)setFooderView{
    
    UIView *fooderView = [[UIView alloc]init];
    
    _footView = fooderView;
    

    fooderView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:fooderView];
    
    [fooderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        
        make.height.mas_offset(50);
    }];
    
    UILabel *moneylabel = [[UILabel alloc]init];
    
    _moneyLabel = moneylabel;
    
    [fooderView addSubview:moneylabel];
    
    [moneylabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(fooderView);
        
        make.left.offset(10);
    }];
    
    moneylabel.textColor = [UIColor redColor];
    
    
    UIButton *agreeButton = [[UIButton alloc]init];
    
    [fooderView addSubview:agreeButton];
    
    agreeButton.backgroundColor = [UIColor hm_colorWithR:252 G:211 B:69];
    
    [agreeButton setTitle:@"选好了" forState:UIControlStateNormal];
    
    [agreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.equalTo(fooderView).offset(0);
        
        make.width.mas_offset(90);
    }];
    
    [agreeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    agreeButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [agreeButton addTarget:self action:@selector(clickAgreeButton) forControlEvents:UIControlEventTouchUpInside];
}

-(void)clickAgreeButton{
    
    
    
}

-(void)setupEmptyView{
    
    UIView *emptyView = [[UIView alloc] init];
    
    _emptyView = emptyView;
    
    emptyView.backgroundColor = [UIColor hm_colorWithR:239 G:239 B:239];
    
    [self.view addSubview:emptyView];
    
    [emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        
        make.top.offset(64);
        
        make.bottom.equalTo(_footView.mas_top);
    }];
    
    UIButton *shopButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    [shopButton setBackgroundImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal];
    
    [shopButton setTitle:@"去逛逛" forState:UIControlStateNormal];
    
    [shopButton setTintColor:[UIColor lightGrayColor]];

    [emptyView addSubview:shopButton];

    
    
    
    [shopButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(emptyView);
        
        make.width.mas_offset(120);
        
        make.height.mas_offset(35);
    }];
    
    UILabel *label = [[UILabel alloc]init];
    
    label.text = @"亲,购物车空空的耶~赶紧挑好吃的吧";
    
    label.textColor = [UIColor lightGrayColor];
    
    [emptyView addSubview:label];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        
        make.bottom.equalTo(shopButton.mas_top).offset(-20);
    }];
    
    UIImageView *imageView = [[UIImageView alloc]init];
    
    [emptyView addSubview:imageView];
    
    imageView.image = [UIImage imageNamed:@"v2_shop_empty"];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        
        make.width.height.mas_offset(80);
        
        make.bottom.equalTo(label.mas_top).offset(-20);
    }];
    
}


-(void)setUpTableView{
    
    [self getShopingArray];
    
    UITableView *shopCarTableView = [[UITableView alloc]init];
    
    _shopCarTableView = shopCarTableView;
    
 
    
    
    shopCarTableView.backgroundColor = [UIColor hm_colorWithR:242 G:242 B:242];
    
    [self.view addSubview:shopCarTableView];
    
    [shopCarTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        
        make.top.offset(64);
        
        make.bottom.equalTo(_footView.mas_top);
        
    }];
    
    shopCarTableView.delegate = self;
    
    shopCarTableView.dataSource = self;
    
    [shopCarTableView registerNib:[UINib nibWithNibName:@"BQShopCarFirstCell" bundle:nil] forCellReuseIdentifier:first];
    [shopCarTableView registerNib:[UINib nibWithNibName:@"BQShopCarSecondCell" bundle:nil] forCellReuseIdentifier:second];
    [shopCarTableView registerNib:[UINib nibWithNibName:@"BQShopCarThirdCell" bundle:nil] forCellReuseIdentifier:third];
    [shopCarTableView registerNib:[UINib nibWithNibName:@"BQShopCarFourthCell" bundle:nil] forCellReuseIdentifier:fourth];
    [shopCarTableView registerNib:[UINib nibWithNibName:@"BQShopCarFifthCell" bundle:nil] forCellReuseIdentifier:fifth];

    
    shopCarTableView.tableFooterView = [UIView new];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return 1;
    }
    
    return 3 + _dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        BQShopCarFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:first forIndexPath:indexPath];
        
        BQAdressModel *model = _adressArray.firstObject;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.model = model;
        
        return cell;
    }else if(indexPath.section == 1 && indexPath.row == 0){
        
        BQShopCarSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:second forIndexPath:indexPath];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
        
    }else if(indexPath.section == 1 && indexPath.row == 1){
        
        BQShopCarThirdCell *cell = [tableView dequeueReusableCellWithIdentifier:third forIndexPath:indexPath];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
        
    }else if(indexPath.section == 1 && indexPath.row == 2){
        
        BQShopCarFourthCell *cell = [tableView dequeueReusableCellWithIdentifier:fourth forIndexPath:indexPath];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
        
    }else{
        
        BQShopCarFifthCell *cell = [tableView dequeueReusableCellWithIdentifier:fifth forIndexPath:indexPath];
        
        BQShopingCarModel *model = _dataArray[indexPath.row - 3];
        
        cell.model = model;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
        
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        return 75;
    }
    
    if (indexPath.section == 1 && indexPath.row == 0) {
        return 60;
    }
    
    return 50;
    
}

-(void)getShopingArray{
    
    FMDatabase *db = [[FMDatabase alloc] initWithPath:localDBPath];
    [db open];
    
        FMResultSet *set = [db executeQuery:@"select * from BeeQuick where count > 0"];
        
        NSMutableArray *dataArray = [NSMutableArray array];
        
        _dataArray = dataArray;
        
        float allprice = 0;
        
        while ([set next]) {
            
            BQShopingCarModel *model = [[BQShopingCarModel alloc]init];
            
            model.name = [set stringForColumn:@"name"];
            
            model.id = [set stringForColumn:@"id"];
            
            model.partner_price = [set stringForColumn:@"partner_price"];
            
            model.count = [set intForColumn:@"count"];
            
            [_dataArray addObject:model];
            
            float price = [model.partner_price doubleValue] * model.count;
            
            allprice += price;
        }
        
        _moneyLabel.text = [NSString stringWithFormat:@"共$ %.2f",allprice];
    
    [db close];
    
    [self.shopCarTableView reloadData];
    
    if (_dataArray.count > 0) {
        self.shopCarTableView.hidden =   NO;
        self.emptyView.hidden = YES;
    }else{
        self.shopCarTableView.hidden =  YES;
        self.emptyView.hidden = NO;
    }
    
}

-(void)loadData{
    
    NSDictionary *para = @{@"call" : @12};
    
    [_AccountMgr POSTWithUrlString:adress_url parameters:para success:^(id responseObject) {
        
        // 获取分类模型数组
        NSArray *dictArray = responseObject[@"data"];
        
        _adressArray = [NSArray yy_modelArrayWithClass:[BQAdressModel class] json:dictArray];
        
        
        [self.shopCarTableView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
  
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        BQAdressVC *adressVC = [[BQAdressVC alloc]init];
        
        [self.navigationController pushViewController:adressVC animated:YES];
        
        adressVC.adressBlock = ^(NSArray *array){
            
            NSMutableArray *blockArray = [NSMutableArray array];
            
            [blockArray addObject:array];
            
            _adressArray = blockArray;
            
            [self.shopCarTableView reloadData];
            
        };
        
    }
    
}

@end
